'use strict';

const createError = require('http-errors');

const models = require('../models');


module.exports = () => {
    let routes = [];
    models.Route.find({}, (err, arr) => {
        if(err) {
            return;
        }
        routes = arr;
    });
    return (req, res, next) => {
        let url = req.path;
        let route = null;
        for(let i = 0, j = routes.length; i < j; i+=1) {
            route = routes[i];
            if(url === route.uri) {
                break;
            }
            route = null;
        }
        if(route) {
            req.routeCfg = route;
            next();
        }else{
            next(createError(404, 'Api不存在'));
        }
    };
};